package cn.unknowpupil.service.system.impl;

import cn.unknowpupil.common.core.exceptions.HealthException;
import cn.unknowpupil.common.core.utils.StringUtils;
import cn.unknowpupil.domain.system.SysPost;
import cn.unknowpupil.mapper.system.SysPostMapper;
import cn.unknowpupil.service.system.ISysPostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created at 2020-10-21 by  w10g <br>
 * Initialize SysPost  ... <br>
 * 岗位信息表 核心业务处理实现类
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {


    /**
     * 新增
     *
     * @param sysPost {@link SysPost}  岗位信息表
     * @return {@link SysPost}
     */
    @Override
    public boolean create(SysPost sysPost) {
        return save(sysPost);
    }

    /**
     * 详情
     *
     * @param id {@code Long}   SysPost ID
     * @return {@link SysPost}
     */
    @Override
    public SysPost info(Long id) {
        return getById(id);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}   SysPost ID
     * @return {@link SysPost}
     */
    @Override
    public boolean delete(Long[] ids) {
        for (Long postId : ids) {
            SysPost post = baseMapper.selectById(postId);
            if (countUserPostById(postId) > 0) {
                throw new HealthException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        return removeByIds(Stream.of(ids).collect(Collectors.toList()));
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    private int countUserPostById(Long postId) {
        return baseMapper.countUserPostById(postId);
    }


    /**
     * 修改
     *
     * @param sysPost {@link SysPost}  岗位信息表
     * @return {@link SysPost}
     */
    @Override
    public boolean update(SysPost sysPost) {
        return updateById(sysPost);
    }


    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @param sysPost {@link SysPost}
     * @return {@code IPage<SysPost>}
     */
    @Override
    public IPage<SysPost> page(int current, int size, SysPost sysPost) {
        IPage<SysPost> page = new Page<>(current, size);
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysPost.getPostCode())) {
            wrapper.like(SysPost::getPostCode, sysPost.getPostCode());
        }
        if (StringUtils.isNotBlank(sysPost.getPostName())) {
            wrapper.like(SysPost::getPostName, sysPost.getPostName());
        }
        if (StringUtils.isNotBlank(sysPost.getStatus())) {
            wrapper.like(SysPost::getStatus, sysPost.getStatus());
        }
        wrapper.orderByAsc(SysPost::getPostSort);
        return super.page(page, wrapper);
    }

    @Override
    public List selectPostAll() {
        return super.list();
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return baseMapper.selectPostListByUserId(userId);
    }

}
